<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="android mio4kon mio">
    <meta name="keyword"  content="">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
          一步步撸出图片加载框架 - Mio4kon的博客 | Mio4kon&#39;s Blog
        
    </title>

    <link rel="canonical" href="http://mio4kon.com/2014/12/19/一步步撸出图片加载框架ImageLoader /">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/hux-blog.min.css">

    <!-- Pygments Highlight CSS -->
    <link rel="stylesheet" href="/css/highlight.css">

    <!-- Custom Fonts -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> -->
    <!-- Hux change font-awesome CDN to qiniu -->
    <link href="https://cdn.staticfile.org/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">


    <!-- Hux Delete, sad but pending in China
    <link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/
    css'>
    -->


    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- ga & ba script hoook -->
    <script></script>
</head>


<!-- hack iOS CSS :active style -->
<body ontouchstart="">

    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">Mio4kon</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/">Home</a>
                    </li>

                    

                        
                    

                        
                        <li>
                            <a href="/about/">About</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/archives/">Archives</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/tags/">Tags</a>
                        </li>
                        
                    
                    
                </ul>
            </div>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>


    <!-- Main Content -->
    
<!-- Image to hack wechat -->
<!-- <img src="http://mio4kon.com/img/icon_wechat.png" width="0" height="0"> -->
<!-- <img src="{{ site.baseurl }}/{% if page.header-img %}{{ page.header-img }}{% else %}{{ site.header-img }}{% endif %}" width="0" height="0"> -->

<!-- Post Header -->
<style type="text/css">
    header.intro-header{
        background-image: url('/img/home-bg.jpg')
    }
</style>
<header class="intro-header" >
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <div class="tags">
                        
                          <a class="tag" href="/tags/#框架,图片加载" title="框架,图片加载">框架,图片加载</a>
                        
                    </div>
                    <h1>一步步撸出图片加载框架</h1>
                    <h2 class="subheading">超实用,超轻量的图片框架</h2>
                    <span class="meta">
                        Posted by Mio4kon on
                        2014-12-19
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

    <!-- Post Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                post-container">

                <p><strong>难得空闲,看了<a href="http://blog.csdn.net/lmj623565791/article/details/41874561" target="_blank" rel="external">鸿洋大神的博客</a>,将代码整合了一下撸出一个简单的图片加载框架,超实用,超轻量,只有2个类(≖ ‿ ≖)✧</strong></p>
<blockquote>
<p>参考链接:  <a href="http://blog.csdn.net/lmj623565791/article/details/41874561" target="_blank" rel="external">鸿洋大神的博客</a></p>
</blockquote>
<p><strong>效果和代码与鸿洋大神博客所写的差不多,主要是加强对这种图片加载框架的理解.</strong></p>
<h2 id="准备工作-图片工具类"><a href="#准备工作-图片工具类" class="headerlink" title="准备工作:图片工具类"></a>准备工作:图片工具类</h2><p><strong>压缩图片</strong> </p>
<pre><code>/**
 * 计算图片相对于ImageView控件的压缩比例
 *
 * @param options
 * @param reqWidth
 * @param reqHeight
 * @return
 */
public static int caculateZoomSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

    //拿到图片的宽高
    int width = options.outWidth;
    int height = options.outHeight;

    //如果图片比ImageView小,不需要压缩了
    int widthZoomSize = 1;
    int heightZoomSize = 1;
    if (reqWidth &gt; width) {
        widthZoomSize = Math.round (reqWidth * 1.0f / width);
    }

    if (reqHeight &gt; height) {
        heightZoomSize = Math.round (reqHeight * 1.0f / height);
    }
    return Math.max (widthZoomSize, heightZoomSize);

}
</code></pre><p><br></p>
<pre><code>/**
 * 压缩图片
 *
 * @param imagePath
 * @param reqWidth
 * @param reqHeight
 * @return
 */
public static Bitmap zoomBitmap(String imagePath, int reqWidth, int reqHeight) {
    BitmapFactory.Options op = new BitmapFactory.Options ();
    op.inJustDecodeBounds = true;
    BitmapFactory.decodeFile (imagePath, op);
    int zoomSize = caculateZoomSize (op, reqWidth, reqHeight);
    Log.d (TAG, &quot;压缩比例:zoomSize:&quot; + zoomSize);
    op.inSampleSize = zoomSize;
    op.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile (imagePath, op);
}
</code></pre><p><strong>从网络下载图片</strong>  </p>
<pre><code> /**
 * 根据url下载图片,自动压缩
 *
 * @param urlStr
 * @param imageview
 * @return
 */
public static Bitmap loadImageFromNet(String urlStr, ImageView imageview) {
    FileOutputStream fos = null;
    InputStream is = null;
    try {
        URL url = new URL (urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection ();
        is = new BufferedInputStream (conn.getInputStream ());
        is.mark (is.available ());

        BitmapFactory.Options opts = new BitmapFactory.Options ();
        opts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeStream (is, null, opts);

        //获取imageview想要显示的宽和高
        ImageSize imageViewSize = getImageViewSize (imageview);
        opts.inSampleSize = caculateZoomSize (opts,
                imageViewSize.width, imageViewSize.height);

        opts.inJustDecodeBounds = false;
        is.reset ();
        bitmap = BitmapFactory.decodeStream (is, null, opts);

        conn.disconnect ();
        return bitmap;

    } catch (Exception e) {
        e.printStackTrace ();
    } finally {
        try {
            if (is != null)
                is.close ();
        } catch (IOException e) {
        }

        try {
            if (fos != null)
                fos.close ();
        } catch (IOException e) {
        }
    }
    return null;
}
</code></pre><a id="more"></a>
<p><strong>从网络下载图片缓存在本地</strong>  </p>
<pre><code>/**
 * @param urlStr 图片url
 * @param file   存储disk address
 * @return
 */
public static boolean loadImageFromNet2Disk(String urlStr, File file) {
    Log.d (TAG, &quot;从网络读取图片到disk,url:&quot; + urlStr);
    FileOutputStream fos = null;
    InputStream is = null;

    try {
        URL url = new URL (urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection ();
        is = conn.getInputStream ();
        fos = new FileOutputStream (file);
        byte[] buf = new byte[512];
        int len = 0;
        while ((len = is.read (buf)) != -1) {
            fos.write (buf, 0, len);
        }
        fos.flush ();
        return true;

    } catch (Exception e) {
        Log.e (TAG, &quot;loadImageFromNet2Disk---&gt;error&quot;);
        e.printStackTrace ();
    } finally {
        try {
            if (is != null) {
                is.close ();
            }
        } catch (IOException e) {
            Log.e (TAG, &quot;loadImageFromNet2Disk---&gt;IO error&quot;);
        }
        try {
            if (fos != null) {
                fos.close ();
            }
        } catch (IOException e) {
            Log.e (TAG, &quot;loadImageFromNet2Disk---&gt;IO error&quot;);
        }
    }

    return false;
}
</code></pre><h2 id="图片加载类-ImageLoader"><a href="#图片加载类-ImageLoader" class="headerlink" title="图片加载类:ImageLoader"></a>图片加载类:ImageLoader</h2><blockquote>
<p>思路:后台有一个线程维护线程池.线程池中存放着读取图片的任务.一旦增加了一个任务,线程池开始执行该任务(通过信号量控制最新的任务先执行),任务执行完成刷新UI线程ImageView图片.</p>
</blockquote>
<p><strong>线程池的任务</strong></p>
<blockquote>
<p>先判断本地是否存有图片,如果有直接加在本地图片,否则网络获取.</p>
<p>判断是否开启本地缓存,如果开启,则先将图片下载本地,在读取本地图片,否则直接下载图片</p>
<p>图片加载完发送图片信息到UIHander刷新UI界面.并释放线程池信号量.  </p>
</blockquote>
<pre><code>/**
 * **创建任务:如果内存有就不会进入到这里**
 *
 * @param path
 * @param imageView
 * @param isFromNet
 * @return
 */
private Runnable buildTask(final String path, final ImageView imageView, final boolean isFromNet) {

    return new Runnable () {
        @Override
        public void run() {
            Bitmap bitmap = null;
            File file = getDiskCacheDir (imageView.getContext (), md5 (path));
            if (file.exists ()) {
                //get image from disk
                bitmap = ImageUtils.loadImageFromDisk (file.getAbsolutePath (), imageView);
            } else {

                if (isDiskCacheEnable) {
                    // load  image from net to disk
                    if (ImageUtils.loadImageFromNet2Disk (path, file)) {
                        Log.d (TAG, &quot; finish loadImageFromNet2Disk &quot;);
                        //get image from disk
                        bitmap = ImageUtils.loadImageFromDisk (file.getAbsolutePath (), imageView);
                    }

                } else {
                    // go directly to get image from net
                    Log.d (TAG, &quot;go directly to get image from net&quot;);
                    bitmap = ImageUtils.loadImageFromNet (path, imageView);
                }
            }

            // image to lruCache
            addBitmapToLruCache (path, bitmap);
            refreashBitmap (path, imageView, bitmap);
            mThreadPoolSemaphore.release ();
        }
    };
}
</code></pre><p><strong>使用框架的主要方法<code>loadImage</code></strong></p>
<blockquote>
<p>包含UIHander 刷新imageView</p>
<p>从内存<code>LruCache</code>中获取图片.如果获取不到添加任务到任务队列</p>
<p>当添加任务给任务队列,此时后台维护线程池的线程就应该从线程池取出任务并执行.</p>
</blockquote>
<pre><code>/**
 * 通过Path给ImageView设置方法
 *
 * @param path
 * @param imageView
 * @param isFromNet
 */
public void loadImage(String path, ImageView imageView, boolean isFromNet) {

    imageView.setTag (path);

    if (mUIHandler == null) {
        //refresh UI
        mUIHandler = new Handler () {
            @Override
            public void handleMessage(Message msg) {
                ImageHolder holder = (ImageHolder) msg.obj;
                Bitmap bitmap = holder.bitmap;
                ImageView imageView = holder.imageView;
                String path = holder.path;

                if (imageView.getTag ().toString ().equals (path)) {
                    imageView.setImageBitmap (bitmap);
                }
            }
        };

    }

    Bitmap bitmap = getBitmapFromLruCache (path); //get bitmap from lruCache

    if (bitmap != null) {
        Log.d (TAG, &quot;读取内存缓存中图片.....&quot;);
        if (imageView.getTag ().toString ().equals (path)) {
            imageView.setImageBitmap (bitmap);
        }
        return;
    }

    addTask (buildTask (path, imageView, isFromNet)); //get bitmap from Disk/Net
}
</code></pre><p><strong>添加任务</strong></p>
<blockquote>
<p>将任务<code>runnable</code>添加到任务队列</p>
<p><code>initSemaphore</code>信号量初始化只有一个,此时占坑阻塞.防止<code>mBackThreadHander</code>为空</p>
<p>发送空消息给后台线程,告诉他可以从线程池中去任务执行</p>
</blockquote>
<pre><code>private synchronized void addTask(Runnable runnable) {
    mTaskQueue.add (runnable);
    try {
        if (mBackThreadHander == null)
            initSemaphore.acquire (); //阻塞等待initBackThread方法完成
    } catch (InterruptedException e) {
        e.printStackTrace ();
    }
    mBackThreadHander.sendEmptyMessage (0x11111);

}        
</code></pre><p><strong>后台线程</strong> </p>
<blockquote>
<p>取任务执行</p>
<p>线程池信号量占一个坑,当坑与线程池数量相同既满了的时候,就阻塞,只有信号量放行了才从任务队列取任务,任务完成释放一个信号量</p>
<p><code>initSemaphore</code>信号量释放,说明初始化后台线程完成</p>
</blockquote>
<pre><code>/**
 * 初始化后台线程,当添加任务后取出任务
 */
private void initBackThread() {

    mBackThread = new Thread () {

        @Override
        public void run() {
            Looper.prepare ();
            //阻塞的,当addTask后才会执行任务
            mBackThreadHander = new Handler () {
                @Override
                public void handleMessage(Message msg) {
                    mThreadPool.execute (getTask ());
                    try {
                        mThreadPoolSemaphore.acquire ();
                    } catch (InterruptedException e) {
                        e.printStackTrace ();
                    }
                    initSemaphore.release ();
                }
            };
            Looper.loop ();
        }
    };
    mBackThread.start ();

}   
</code></pre><p><strong>LruCache初始化</strong></p>
<blockquote>
<p>可运行内存的1/8用于内存缓存</p>
</blockquote>
<pre><code>/** 初始化LruCache **/
int maxMemory = (int) Runtime.getRuntime ().maxMemory ();
int cacheMemory = maxMemory / 8;
mLruCache = new LruCache&lt;String, Bitmap&gt; (cacheMemory) {
    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes () * value.getHeight ();
    }
};
</code></pre><h2 id="使用方法"><a href="#使用方法" class="headerlink" title="使用方法"></a>使用方法</h2><pre><code>imageLoader = ImageLoader.getInstance ();  
imageLoader.loadImage (url,imageView,true);
</code></pre><p><code>项目地址:</code> <a href="https://github.com/mio4kon/Mio4kon-Imageload" target="_blank" rel="external">Github</a>     </p>


                <hr>

                

                <ul class="pager">
                    
                        <li class="previous">
                            <a href="/2014/12/30/一步步撸出viewpagerindicator精简版/" data-toggle="tooltip" data-placement="top" title="一步步撸出ViewpagerIndicator(精简版)">&larr; Previous Post</a>
                        </li>
                    
                    
                        <li class="next">
                            <a href="/2014/12/01/Wiki/" data-toggle="tooltip" data-placement="top" title="Android wiki">Next Post &rarr;</a>
                        </li>
                    
                </ul>

                

                
                <!-- disqus 评论框 start -->
                <div class="comment">
                    <div id="disqus_thread" class="disqus-thread"></div>
                </div>
                <!-- disqus 评论框 end -->
                

            </div>
    <!-- Side Catalog Container -->
        
            <div class="
                col-lg-2 col-lg-offset-0
                visible-lg-block
                sidebar-container
                catalog-container">
                <div class="side-catalog">
                    <hr class="hidden-sm hidden-xs">
                    <h5>
                        <a class="catalog-toggle" href="#">CATALOG</a>
                    </h5>
                    <ul class="catalog-body"></ul>
                </div>
            </div>
        

    <!-- Sidebar Container -->

            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                
                <section>
                    <!-- no hr -->
                    <h5><a href="/tags/">FEATURED TAGS</a></h5>
                    <div class="tags">
                       
                          <a class="tag" href="/tags/#框架,图片加载" title="框架,图片加载">框架,图片加载</a>
                        
                    </div>
                </section>
                

                <!-- Friends Blog -->
                
                <hr>
                <h5>FRIENDS</h5>
                <ul class="list-inline">

                    
                        <li><a href="http://lrd.ele.me/" target="_blank">lrd ele&#39;s Blog</a></li>
                    
                </ul>
                
            </div>

        </div>
    </div>
</article>




<!-- disqus 公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES * * */
    var disqus_shortname = "hexo-theme-huxblog";
    var disqus_identifier = "http://mio4kon.com/2014/12/19/一步步撸出图片加载框架ImageLoader /";
    var disqus_url = "http://mio4kon.com/2014/12/19/一步步撸出图片加载框架ImageLoader /";

    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<!-- disqus 公共JS代码 end -->




<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>
<!-- anchor-js, Doc:http://bryanbraun.github.io/anchorjs/ -->
<script>
    async("https://cdn.bootcss.com/anchor-js/1.1.1/anchor.min.js",function(){
        anchors.options = {
          visible: 'always',
          placement: 'right',
          icon: '#'
        };
        anchors.add().remove('.intro-header h1').remove('.subheading').remove('.sidebar-container h5');
    })
</script>
<style>
    /* place left on bigger screen */
    @media all and (min-width: 800px) {
        .anchorjs-link{
            position: absolute;
            left: -0.75em;
            font-size: 1.1em;
            margin-top : -0.1em;
        }
    }
</style>



    <!-- Footer -->
    <!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                
                
                

                
                    <li>
                        <a target="_blank" href="http://weibo.com/u/3198010790">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-weibo fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                

                
                    <li>
                        <a target="_blank"  href="https://github.com/Mio4kon">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                

                </ul>
                <p class="copyright text-muted">
                    Copyright &copy; Mio4kon 2019 
                    <br>
                    Theme by <a href="http://huangxuan.me">Hux</a> 
                    <span style="display: inline-block; margin: 0 5px;">
                        <i class="fa fa-heart"></i>
                    </span> 
                    Ported by <a href="http://blog.kaijun.rocks">Kaijun</a> | 
                    <iframe
                        style="margin-left: 2px; margin-bottom:-5px;"
                        frameborder="0" scrolling="0" width="91px" height="20px"
                        src="https://ghbtns.com/github-btn.html?user=kaijun&repo=hexo-theme-huxblog&type=star&count=true" >
                    </iframe>
                </p>
            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/hux-blog.min.js"></script>


<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- 
     Because of the native support for backtick-style fenced code blocks 
     right within the Markdown is landed in Github Pages, 
     From V1.6, There is no need for Highlight.js, 
     so Huxblog drops it officially.

     - https://github.com/blog/2100-github-pages-now-faster-and-simpler-with-jekyll-3-0  
     - https://help.github.com/articles/creating-and-highlighting-code-blocks/    
-->
<!--
    <script>
        async("http://cdn.bootcss.com/highlight.js/8.6/highlight.min.js", function(){
            hljs.initHighlightingOnLoad();
        })
    </script>
    <link href="http://cdn.bootcss.com/highlight.js/8.6/styles/github.min.css" rel="stylesheet">
-->


<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("http://mio4kon.com/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>


<!-- Google Analytics -->




<!-- Baidu Tongji -->


<!-- Side Catalog -->

<script type="text/javascript">
    function generateCatalog (selector) {
        var P = $('div.post-container'),a,n,t,l,i,c;
        a = P.find('h1,h2,h3,h4,h5,h6');
        a.each(function () {
            n = $(this).prop('tagName').toLowerCase();
            i = "#"+$(this).prop('id');
            t = $(this).text();
            c = $('<a href="'+i+'" rel="nofollow">'+t+'</a>');
            l = $('<li class="'+n+'_nav"></li>').append(c);
            $(selector).append(l);
        });
        return true;    
    }

    generateCatalog(".catalog-body");

    // toggle side catalog
    $(".catalog-toggle").click((function(e){
        e.preventDefault();
        $('.side-catalog').toggleClass("fold")
    }))

    /*
     * Doc: https://github.com/davist11/jQuery-One-Page-Nav
     * Fork by Hux to support padding
     */
    async("/js/jquery.nav.js", function () {
        $('.catalog-body').onePageNav({
            currentClass: "active",
            changeHash: !1,
            easing: "swing",
            filter: "",
            scrollSpeed: 700,
            scrollOffset: 0,
            scrollThreshold: .2,
            begin: null,
            end: null,
            scrollChange: null,
            padding: 80
        });
    });
</script>





<!-- Image to hack wechat -->
<img src="http://mio4kon.com/img/icon_wechat.png" width="0" height="0" />
<!-- Migrate from head to bottom, no longer block render and still work -->

</body>

</html>
